/*
 * ProgramChair.java
 * Author: Russell Sowell
 */

package superConference;

import java.util.ArrayList;
import java.util.List;

/**
 * Class used to represent ProgramChairs.
 * @author Russell
 * @updated 12/3/2013
 */
public class ProgramChair extends AbstractUser
{
	/**
	 * Constructor to initialize base values
	 * @param userID ID associated with programchair
	 * @param email Email associated with programchair
	 * @param name Name associated with programchair
	 */
	ProgramChair(int userID, String email, String name) 
	{
		super(userID, email, name);
	}
	
	/**
	 * Accepts or rejects a paper due to input from the user. 
	 * @param accept Boolean value that is the ProgramChair's decision on the paper.
	 * @param theAuthorList The Authorlist to find the papers.
	 * @param id The id of the paper to be accepted/rejected.
	 */
	public boolean acceptOrReject(boolean accept, List<Author> theAuthorList, int id)
	{
		boolean found = false;
		for(Author theAuthor : theAuthorList)
		{
			for(Paper p : theAuthor.getListOfPaper())
			{
				if(p.getId() == id)
				{
					p.accept(accept);
					found = true;
//					return found;
				}
			}
		}
		return found;
	}
	
	/**
	 * Designates a reviewer as a subprogram chair. Per BR06.
	 * @param theReviewer The reviewer to be appointed to SubProgramChair.
	 * @param theID The new ID number for this user's new login. Should be Conference user count + 1.
	 * @return Returns the new SubProgramChair user object to be inserted into conference.
	 */
	public SubProgramChair designateSubChair(Reviewer theReviewer, int theID)
	{
		SubProgramChair theSubChair = new SubProgramChair(theID, theReviewer.getEmail(), 
														theReviewer.getName());
		return theSubChair;
	}
	
	/**
	 * Assigns a paper to a SubProgramChair if the chair does not have 4 papers already and is not the
	 * author of the paper in question.
	 * @param theSubChair SubProgramChair for the paper to be added to.
	 * @param theAuthorList AuthorList used to find the correct paper
	 * @param id Id of paper within authors.
	 */
	public void assignPaper(List<SubProgramChair> theChairs, int chairID, List<Author> theAuthorList, int paperID)
	{
		SubProgramChair theSubChair = new SubProgramChair(0, "", "");
		for(SubProgramChair spc : theChairs)
		{
			if(spc.getUserID() == chairID)
			{
				theSubChair = spc;
				break;
			}
		}
		
		
		ArrayList<Paper> theSubChairPapers = theSubChair.getPaper();
		
		//Find the paper within the author list.
		for(Author theAuthor : theAuthorList)
		{
			for(Paper p : theAuthor.getListOfPaper())
			{
				if(p.getId() == paperID)
				{
					//Has the subchair reached their paper limit(4 BR18)? Are they the author of this paper?
					if (theSubChairPapers.size() < 4 && !theSubChairPapers.contains(p))
					{
						theSubChair.addPaper(p);
					}
					return;
				}
			}
		}
	}
	
	/**
	 * Creates a String for paper list.
	 * @param theAuthorList List of authors to grab papers from
	 * @return String to be displayed
	 */
	public String paperListString(List<Author> theAuthorList)
	{
		StringBuilder sb = new StringBuilder();
		for(Author a : theAuthorList)
		{
			for(Paper p : a.getListOfPaper())
			{
				sb.append("ID: ");
				sb.append(p.getId());
				sb.append("\t");
				sb.append(p.toString());
				sb.append("\n");
			}
		}
		if (sb.length() == 0)
		{
			sb.append("No Papers in system yet");
		}
		return sb.toString();
	}
	
	/**
	 * Creates a String to be displayed in GUI for subChair papers
	 * @param theSubChairs The subchairs with papers
	 * @return String of Subchairs with their papers
	 */
	public String subChairString(List<SubProgramChair> theSubChairs)
	{
		StringBuilder sb = new StringBuilder();
		for(SubProgramChair subChair : theSubChairs)
		{
			sb.append("SubChair\nID: ");
			sb.append(subChair.getUserID());
			sb.append("\tName: ");
			sb.append(subChair.getName());
			sb.append("\n");
			
			for(Paper p : subChair.getPaper())
			{
				sb.append("Paper ID: ");
				sb.append(p.getId());
				sb.append("\t");
				sb.append(p.toString());
				sb.append("\n");
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
}
